<script setup lang="ts">
import {User, Lock} from '@element-plus/icons-vue';
import {useRoute, useRouter} from "vue-router";
import {reactive, ref} from "vue";
import useUserStore from "@/store/modules/user.ts";
import { ElNotification } from 'element-plus'
const route = useRoute()
const router = useRouter()

const loading = ref(false)
//用户名和密码
const loginForm = reactive({username: 'admin', password: '111111'})
const useStore = useUserStore()
const login = async () => {
  loading.value = true
  try{
    await useStore.userLogin(loginForm)
     router.push('/')
    //登录成功提示
    ElNotification({
      type: 'success',
      message: '登录成功！',
    })
    //登录成功,加载效果也消失
    loading.value = false
  }catch (error) {
    //登陆失败加载效果消失
    loading.value = false
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
}
</script>

<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form" :model="loginForm" ref="loginForms">
          <h1>Hello</h1>
          <h2>欢迎来到洋洋平台</h2>
          <el-form-item>
            <el-input :prefix-icon="User" v-model="loginForm.username"/>
          </el-form-item>
          <el-form-item>
            <el-input :prefix-icon='Lock' type="password" v-model="loginForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="login_btn" type="primary" size="default" @click="login" :loading="false">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>


<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    left: 10%;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
